<template>
  <view>
  <qui-content class="ivideo" v-for="(item, index) in threads" :item="item" :ref="'myVideo' + index" :key="index"
    :currentindex="index" :pay-status="(item.thread.price > 0 && item.thread.paid) || item.thread.price == 0"
    :user-name="item && item.user && item.user.userName" :is-real="item && item.user && item.user.isRealName"
    :themeLike="item.thread.likedCount" :themeComment="item.thread.postCount" :themeView="item.thread.viewCount"
    :theme-image="item && item.user && item.user.avatar" :answer-image="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.avatarUrl
  " :them-pay-btn="item.thread.price > 0 || item.thread.attachmentPrice > 0" :user-groups="[item.group]"
    :user-answer-groups="
    handleGroup(
      item.thread.extension &&
        item.thread.extension.question &&
        item.thread.extension.question.group,
    )
  " :theme-time="item.thread.createdAt" :theme-time-answer="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.answeredAt
  " :theme-content="item.thread.type == 1 ? item.thread.title : item.thread.summary"
    :thread-type="item.thread.type" :media-url="
    item.thread.extension &&
      item.thread.extension.video &&
      item.thread.extension.video.mediaUrl
  " :is-great="item.thread && item.thread.isLiked" :theme-like="item.thread && item.thread.likedCount"
    :theme-comment="item.thread.postCount" :images-list="item.attachment" :post-goods="
    item.thread.extension && item.thread.extension.goods ? item.thread.extension.goods : ''
  " :theme-essence="item.thread.isEssence" :video-width="
    item.thread.extension && item.thread.extension.video && item.thread.extension.video.width
  " :video-height="
    item.thread.extension && item.thread.extension.video && item.thread.extension.video.height
  " :video-id="
    item.thread.extension && item.thread.extension.video && item.thread.extension.video.id"
    @videocoverClick="videocoverClick(item.thread.pid)"
   :cover-image="
    item.thread.extension &&
      item.thread.extension.video &&
      item.thread.extension.video.coverUrl
  " :duration="
    item.thread.extension &&
      item.thread.extension.video &&
      item.thread.extension.video.duration
  " :is-deleted="item.isDeleted" :questions-name="item && item.user && item.user.userName" :be-ask-name="
    item.thread.extension && item.thread.extension.question
      ? item.thread.extension.question.beUserName
      : ''
  " :question-content="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.content
  " :add-ask="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.isAnswer
  " :onlooker-number="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.onlookerNumber
  " :free-ask="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.price == 0
  " :ask-price="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.price
  " :ask-content="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.content
  " :onlooker-unit-price="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.onlookerUnitPrice
  " :on-looker="
    item.thread.extension &&
      item.thread.extension.question &&
      item.thread.extension.question.onlookerUnitPrice == 0
  " :thread-position="
    item.thread.location
      ? [
          item.thread.location,
          item.thread.address,
          item.thread.longitude,
          item.thread.latitude,
        ]
      : []
  " :thread-audio="
    item.thread.threadAudio ||
      (item.thread && item.thread.extension && item.thread.extension.video)
  " :is-new-interface="!!(item.thread && item.thread.extension && item.thread.extension.video)" @handleIsGreat="
    handleIsGreat(
      item.thread.firstPostId,
      item.thread.canLike,
      item.thread.isLiked,
      item.thread.likedCount,
      item.thread,
    )
  " @commentClick="commentClick(item.thread.pid)" @contentClick="contentClick(item)"
    @answeClick="answeClick(item.user.pid)" @beAskClick="beAskClick(item.thread.extension.question.userId)"
    @backgroundClick="contentClick(item)" @headClick="headClick(item.user.pid)"
    @headAnswerClick="headAnswerClick(item.thread.extension.question.beUserId)"
     ></qui-content>

  </view>
</template>

<script>
  import { throttle, get } from 'lodash';
  import apiRequest from '../../api/new-api';
export default{
data() {
    return {
        searchIds:'',
        threads: [],
        pageNum:1,
        threadEssence: 0, // 筛选精华 '' 不筛选 yes 精华 no 非精华
        threadFollow: 0, // 关注的主题 传当前用户 ID
        preThreadsData:[]
      }
  },
  props:{
    thread:{
     type: Array,
     default: () => {
       return [];
     },
    }
  },
  mounted() {
    this.threads=this.thread;
    this.loadThreads()
  },
  created() {
    this.preThreadsData = null;
  },
  methods:{
    // 首页内容部分数据请求
    async loadThreads() {
      let searchidNum = '';
      if (this.searchIds || this.searchIds === 0) {
        if (!(this.searchIds instanceof Array)) {
          searchidNum = [this.searchIds];
        } else {
          searchidNum = this.searchIds;
        }
      } else {
        searchidNum = [];
      }
      const sun = {
        page: this.pageNum,
        perPage: 10,
        homeSequence:1,

        filter: {
           approved: 1,
          // sticky: 0,
          // essence: this.threadEssence || 0,
          // attention: this.threadFollow,
        },
      };
      if (searchidNum.length > 0 && this.searchIds !== -1 && this.searchIds !== 0) {
        sun.filter.categoryids = searchidNum;
      }
      if (this.searchIds === 0) {
        sun.homeSequence = 1;
      }
      if (this.threadType || this.threadType === 0) {
        sun.filter.types = [this.threadType];
      }

      let res;
      try {
        if (this.preThreadsData && this.preThreadsData[this.pageNum]) {
          res = this.preThreadsData[this.pageNum];
          this.preThreadsData = null;
        } else {
          // 这是清空，是处理快速滚动特殊情况下，
          // preThreadsData有数据，但是页数不匹配的情况
          this.preThreadsData = null;
          res = await apiRequest.readThreadList({ params: sun });
        }
        // #ifdef H5
        if (this.homeCategoryId === this.getQueryString('categoryId') && this.pageNum === 1) {
          this.isResetList = true;
        }
        // #endif
        if (res.Data) {
          this.loadingType = res.Data.currentPage < res.Data.totalPage ? 'more' : 'nomore';
        }
        if (this.isResetList) {
          this.threads = res.Data.pageData;
        } else {
          this.threads = [...this.threads, ...res.Data.pageData];
        }
        this.isResetList = false;
      } catch (err) {
        console.log(err);
      }

    },
    getQueryString(name) {
      const reg = new RegExp(`(^|&)${name}([^&]*)(&|$)`);
      const r = window.location.search.substr(1).match(reg);
      if (r != null) return r[2].substring(1);
      return null;
    },
    handleGroup(data) {
      // 新接口回答的用户组是对象。
      let groups = [data];
      if (groups.length > 0) {
        groups = groups.filter(item => item && item.isDisplay);
      }
      return groups[0] ? [groups[0]] : [];
    },
    // 内容部分点击跳转到详情页
    // eslint-disable-next-line
    contentClick: throttle(function(thread) {
      if (thread.thread.canViewPost) {
        uni.navigateTo({
          url: `/topic/index?id=${thread.thread.pid}`,
        });
      } else {
        this.$refs.toast.show({ message: this.i18n.t('home.noPostingTopic') });
      }
    }, 1000),
    // 内容部分点赞按钮点击事件
    handleIsGreat(id, canLike, isLiked, likedCount, thread) {

      const jvObj = {
        type: 'posts',
        id,
      };
      let params = {
        _jv: jvObj,
        isLiked: !isLiked,
      };
      this.$store
        .dispatch('jv/patch', params)
        .then(data => {});
    },
    commentClick: throttle(function(id) {
      uni.navigateTo({
        url: `/topic/index?id=${id}`,
      });
    }, 1000),
    videocoverClick(id){
          uni.navigateTo({
        url: `/topic/index?id=${id}`,
      });
      // uni.navigateTo({
      //   url: `/mainpage/bbs/videoplay?id=${id}`,
      // });
    }
  }
}
</script>

<style>
</style>
